package com.application.Tree;

import java.io.File;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ReadWriteXML.DOMUltil;
import ReadWriteXML.ReadXML;
import ReadWriteXML.WriteXML;
import android.graphics.Canvas;
import android.util.Log;

public class TreeNode extends INode {

	ArrayList<INode> nodes = new ArrayList<INode>();

	public TreeNode(){
		
	}
//	public TreeNode(long id, String text, float x, float y, String user) {
//		super(id, text, x, y, user);
//
//	}
	public TreeNode(long id, String title,String text,float x,float y, String user){
		super(id,title, text, x, y, user);
	}
	

	@Override
	public void add(INode node) {
		
		node.setDepth(this.depth + 1);
		nodes.add(node);
		Log.d("Phu", "Đã add node với id="+node.id);
	}	
	
	@Override
	public void remove(INode node) {

		for (INode i : nodes) {
			i.remove(node);
		}
		nodes.remove(node);
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int size = nodes.size();
		float d,h,sin, alpha;
		for (INode node : nodes) {
			ConnectorLine line = new ConnectorLine(this.x + this.width / 2,
					this.y + this.heigh, node.x + node.width / 2, node.y+node.heigh/2);
			line.onDraw(canvas);
			node.onDraw(canvas);
//			d=line.getY2()-line.getY1();
//			h=(float) Math.sqrt(d*d+line.getX1()+line.getX2());
//			sin=d/h;
//			alpha=(float) Math.asin(sin);
//			alpha+=45;
		}
	}

	@Override
	public INode getById(long id) {
		INode rs = null;
		if (this.id == id){
			Log.d("Phu", "Tìm node: getById: id="+this.id);
			return this;
		}

		for (INode node : nodes) {
			rs = node.getById(id);
			if (rs != null) {
				break;
			}
		}
		return rs;
	}

	@Override
	public ArrayList<INode> getChildren() {
		return nodes;
	}

	public INode getCollision(float x, float y) {
		INode rs = null;
		if (this.isCollision(x, y))
			return this;
		for (INode node : nodes) {
			rs = node.getCollision(x, y);
			if (rs != null)
				return rs;
		}
		return null;
	}

	public void setPostion(float x, float y) {
		super.setPostion(x, y);
	}

	public void moveAll(float moveX, float moveY) {
		super.setPostion(this.x + moveX, this.y + moveY);
		for (INode node : nodes) {
			node.moveAll(moveX, moveY);// (node.x+moveX, node.y+moveY);
		}
	}

	public void autoArrange() {
		int size = this.nodes.size();
		INode cl = null;
		for (int i = 0; i < size; i++) {

			int z = 0;
			if (i > 0)
				z = nodes.get(i - 1).getSumChildren();
			 if(z==0)
				 z=1;
			if (i > 0) {
				nodes.get(i).setX(nodes.get(i - 1).x + z * (this.width + 10));
				nodes.get(i).setY(this.y + this.heigh + 20);
			} else {
				nodes.get(i).setX(this.x );
				nodes.get(i).setY(this.y + this.heigh + 20);
			}
			nodes.get(i).autoArrange();

		}
	}

	@Override
	public int getSumChildren() {
		int rs=this.nodes.size();
		for(INode node:this.nodes){
			rs+=node.getSumChildren();
		}
		return rs;
	}

	@Override
	public int getNodeHeight() {
		int rs=0;
		if(this.nodes.size()>0)
			rs++;
		return 0;
	}

	@Override
	public Element toXmlText(Document doc) {
		Element e=doc.createElement("TreeNode");
		e.setAttribute("ID", this.id+"");
		e.setAttribute("title", this.title);
		e.setAttribute("text", this.text+"");
		e.setAttribute("x", this.x+"");
		e.setAttribute("y", this.y+"");
		e.setAttribute("user",this.user);
		for(INode node:this.nodes){
			e.appendChild(node.toXmlText(doc));
		}		
		return e;
	}
	public synchronized void writeXML(String path){
		File f = new File(path);
		if(f.exists())
			f.delete();
		Document doc = null;;
		doc = WriteXML.insertRoot(doc, path, "Root");
		doc.getFirstChild().appendChild(this.toXmlText(doc));
		DOMUltil.writeXmlToFile(path, doc);
	}

	@Override
	public INode readXML(Element element) {
		this.id=Long.parseLong(element.getAttribute("ID"));
		this.title=element.getAttribute("title");
		this.text=element.getAttribute("text");
		this.x=Float.parseFloat(element.getAttribute("x"));
		this.y=Float.parseFloat(element.getAttribute("y"));
		this.user=element.getAttribute("user");
		NodeList nodeList=element.getChildNodes();
		for( int i=0;i<nodeList.getLength();i++){
			TreeNode n=new TreeNode();
			this.add(n.readXML((Element) nodeList.item(i)));
		}
		return this;
	}
	public void readXml(String path) {
		Document doc = ReadXML.BuildXML(path);		
		this.readXML((Element) doc.getFirstChild().getFirstChild());
	}
	public boolean clearChildren(){
		for(INode node:this.nodes){
			node.clearChildren();
			this.remove(node);
		}
		return true;
	}
	@Override
	public void zoomIn() {
		if (isZoomIn) {
			isZoomIn = false;
			this.heigh /= 2;
			this.width /= 2;

			for (INode node : nodes) {
				node.zoomIn();
				node.x -= node.width / 2;
				node.y -= node.heigh / 2;
			}
		} else {
			isZoomIn = true;
			this.heigh *= 2;
			this.width *= 2;

			for (INode node : nodes) {
				node.zoomIn();
				node.x += node.width / 2;
				node.y += node.heigh / 2;
			}
		}
	}
	@Override
	public String toParse() {
		return super.toParse();
	}
	@Override
	public boolean haveOtherChildren(String myUserName) {
		boolean rs=false;
		if(this.user!=myUserName)
			return true;
		for(INode node:nodes){
			rs=node.haveOtherChildren(myUserName);
			if(rs==true)
				break;
		}
		return rs;
	}
	
}
